home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_tut / book.ada < prev    next >
Text File  |  1996-01-30  |  12KB  |  296 lines

  1. -- BOOK.ADA   Ver. 3.00   22-AUG-1994   Copyright 1988-1994 John J. Herro
  2. -- Software Innovations Technology
  3. -- 1083 Mandarin Drive NE, Palm Bay, FL  32905-4706   (407)951-0233
  4. --
  5. -- This program creates two printable "book" files from the tutorial text in
  6. -- ADA_TUTR.DAT.  This is by no means required for the course, but is provided
  7. -- because several users have asked for it.  Be prepared to print about 500
  8. -- pages!  For the format of the input data file, please see the preliminary
  9. -- comments in ADA_TUTR.ADA.
  10. --
  11. with Direct_IO, Text_IO; use Text_IO;
  12. procedure Book is
  13.    subtype Block_Subtype is String(1 .. 64);
  14.    package Random_IO is new Direct_IO(Block_Subtype);
  15.    IxSize     : constant := 35;               -- Number of blocks in the index.
  16.    Data_File  : Random_IO.File_Type;   -- The file from which screens are read.
  17.    Prnt       : File_Type;                   -- The output file, to be printed.
  18.    Block      : Block_Subtype;               -- Block read from the input file.
  19.    Vpos       : Integer;                        -- Number of the current block.
  20.    Hpos       : Integer;              -- Current position within current block.
  21.    Highest_SN : Integer;                -- Highest screen number in the course.
  22.    Middle_SN  : Integer;          -- Screen number when we change output files.
  23.    Indx       : String(1 .. 64*IxSize);            -- Index from the Data File.
  24.    Answer     : String(1 .. 80);                 -- User response to questions.
  25.    Len        : Integer;                                   -- Length of Answer.
  26.    File_OK    : Boolean := False;    -- True when data file opens successfully.
  27.    Legal_Note : constant String := " Copyright 1988-94 John J. Herro ";
  28.                        -- Legal_Note isn't used by the program, but it causes
  29.                        -- most compilers to place this string in the .EXE file.
  30.    procedure Open_Input_File is separate;
  31.    procedure Open_Output_File(S: in String) is separate;
  32.    procedure Print_Instructions is separate;
  33.    procedure Print_Title_Page is separate;
  34.    procedure Print_Screen(SN : in Integer) is separate;
  35. begin
  36.    Open_Input_File;
  37.    if File_OK then
  38.       Print_Instructions;
  39.       Open_Output_File("FIRST");
  40.       Print_Title_Page;
  41.       Middle_SN := (101 + Highest_SN)/2;
  42.       for SN in 101 .. Highest_SN loop
  43.          if SN = Middle_SN then
  44.             Close(Prnt);
  45.             Open_Output_File("SECOND");
  46.          end if;
  47.          Print_Screen(SN);
  48.       end loop;
  49.       Put_Line("Both book files are created and ready for printing.");
  50.       Random_IO.Close(Data_File);
  51.       Close(Prnt);
  52.    end if;
  53. end Book;
  54.  
  55. separate (Book)
  56. procedure Open_Input_File is
  57.    Data_File_Name : constant String := "ADA_TUTR.DAT";
  58. begin
  59.    Random_IO.Open(Data_File, Random_IO.In_File, Data_File_Name);
  60.    for I in 1 .. IxSize loop             -- Read index from start of Data File.
  61.       Random_IO.Read(Data_File, Item => Block, From => Random_IO.Count(I));
  62.       Indx(64*I - 63 .. 64*I) := Block;
  63.    end loop;
  64.    Highest_SN := Integer'Value(Indx(6 .. 8));
  65.    File_OK := True;
  66. exception
  67.    when Random_IO.Name_Error =>
  68.       Put("I'm sorry.  The file " & Data_File_Name & " seems to be missing.");
  69.    when others =>
  70.       Put("I'm sorry.  The file " & Data_File_Name);
  71.       Put_Line(" seems to have the wrong form.");
  72. end Open_Input_File;
  73.  
  74.  
  75.  
  76. separate (Book)
  77. procedure Open_Output_File(S: in String) is
  78.    OK : Boolean := False;                 -- True when file opens successfully.
  79. begin
  80.    Put_Line("Please type the name of the output file for the " & S & " half");
  81.    Put("of the tutorial:  ");
  82.    Get_Line(Answer, Len);
  83.    while not OK loop
  84.       begin
  85.          Create(File => Prnt, Mode => Out_File, Name => Answer(1 .. Len));
  86.          OK := True;
  87.       exception
  88.          when others => null;
  89.       end;
  90.       if not OK then
  91.          Put_Line("Unable to create file.  Please retype name:  ");
  92.          Get_Line(Answer, Len);
  93.       end if;
  94.    end loop;
  95.    New_Line(2);
  96. end Open_Output_File;
  97.  
  98. separate (Book)
  99. procedure Print_Instructions is
  100. begin
  101.    Put_Line("This program creates two printable ""book"" files from the");
  102.    Put_Line("tutorial text in ADA_TUTR.DAT.  This is by no means required");
  103.    Put_Line("for the course, but is provided because several users have");
  104.    Put_Line("asked for it.  Be prepared to print about 500 pages!");
  105.    New_Line(2);
  106. end Print_Instructions;
  107.  
  108.  
  109.  
  110. separate (Book)
  111. procedure Print_Title_Page is
  112. begin
  113.    New_Page(Prnt);
  114.    New_Line(Prnt);
  115.    Put_Line(Prnt, "   AAA   DDDD    AAA          TTTTT  U   U  TTTTT  RRRR");
  116.    Put_Line(Prnt, "  A   A  D   D  A   A           T    U   U    T    R   R");
  117.    Put_Line(Prnt, "  AAAAA  D   D  AAAAA   ===     T    U   U    T    RRRR");
  118.    Put_Line(Prnt, "  A   A  D   D  A   A           T    U   U    T    R  R");
  119.    Put_Line(Prnt, "  A   A  DDDD   A   A           T     UUU     T    R   R");
  120.    New_Line(Prnt);
  121.    Put_Line(Prnt, "This is a copy of the tutorial text from ADA-TUTR, The");
  122.    Put_Line(Prnt, "Interactive Ada Tutor, ver. 3.00.  BEGIN WITH SCREEN 104.");
  123.    New_Line(Prnt);
  124.    Put_Line(Prnt, "            Copyright 1988-1994 John J. Herro");
  125.    New_Line(Prnt);
  126.    Put_Line(Prnt, "You may copy this book, in printed or machine-readable");
  127.    Put_Line(Prnt, "form, if you observe the Shareware notice in Screen 104.");
  128.    Put_Line(Prnt, "Please distribute complete copies of the ADA-TUTR program");
  129.    Put_Line(Prnt, "along with this book.  If you don't have a copy of");
  130.    Put_Line(Prnt, "ADA-TUTR, send $10 for a trial copy or $30 for a");
  131.    Put_Line(Prnt, "registered copy for full use by one individual.  Send $5");
  132.    Put_Line(Prnt, "more if you prefer a 3.5"" diskette.");
  133.    New_Line(Prnt);
  134.    Put_Line(Prnt, "             Software Innovations Technology");
  135.    Put_Line(Prnt, "                  1083 Mandarin Drive NE");
  136.    Put_Line(Prnt, "                 Palm Bay, FL  32905-4706");
  137.    New_Line(Prnt);
  138.    Put_Line(Prnt, "                      (407) 951-0233");
  139.    New_Page(Prnt);
  140.    New_Line(Prnt);
  141. end Print_Title_Page;
  142.  
  143. separate (Book)
  144. procedure Print_Screen(SN : in Integer) is
  145.    Expanding  : Boolean := False;       -- True when expanding multiple spaces.
  146.    Prompting  : Boolean := False;      -- True for first character in a prompt.
  147.    Bold       : Boolean := False;        -- True when text is being emphasized.
  148.    Out1, Out2 : String(1 ..120) := (others => ' ');    -- Lines of output text.
  149.    Place      : Integer := 1;         -- Current position within Out1 and Out2.
  150.    Limit      : Integer;           -- Position of last non-space char. in Out2.
  151.    Line_Num   : Integer := 1;                  -- Current line being displayed.
  152.    Space      : constant String(1 .. 69) := (others => ' ');
  153.    procedure Show(C : in Character) is separate;
  154.    procedure Screen_Char is separate;
  155.    procedure End_Of_Screen is separate;
  156. begin
  157.    if SN = 103 then
  158.       Put(Prnt, Space(1 .. 27) & "*** X TAKES YOU HERE. ***");
  159.       Put_Line(Prnt, Space(1 .. 17) & "Screen 103");
  160.    else
  161.       Put_Line(Prnt, Space & "Screen" & Integer'Image(SN));
  162.    end if;
  163.    New_Line(Prnt, 2);
  164.    Vpos := 95*(Character'Pos(Indx(SN*4 - 394)) - 32) +        -- Point to start
  165.                Character'Pos(Indx(SN*4 - 393)) - 32;          -- of current
  166.    Hpos := Character'Pos(Indx(SN*4 - 392)) - 32;              -- screen.
  167.    Random_IO.Read(Data_File, Item => Block, From => Random_IO.Count(Vpos));
  168.    while Block(Hpos) /= '[' or Expanding loop     -- [ starts the control info.
  169.       Screen_Char;
  170.    end loop;
  171.    End_Of_Screen;
  172. end Print_Screen;
  173.  
  174. separate (Book.Print_Screen)
  175. procedure Show(C : in Character) is
  176. begin
  177.    Out1(Place) := C;
  178.    if Bold then
  179.       Out2(Place) := '-';
  180.    end if;
  181.    Place := Place + 1;
  182. end Show;
  183.  
  184.  
  185.  
  186. separate (Book.Print_Screen)
  187. procedure Screen_Char is
  188.    procedure Process_Char is separate;
  189. begin
  190.    if Expanding then
  191.       for I in Integer range 1 .. Character'Pos(Block(Hpos)) - 32 loop
  192.          Show(' ');
  193.       end loop;
  194.       Expanding := False;
  195.    elsif Prompting then
  196.       Prompting := False;
  197.       if Block(Hpos) = 'b' then
  198.          Put(Prnt, "Please type a space to go on, or B to go back.");
  199.       elsif Block(Hpos) = 'q' then
  200.          PUT(Prnt, "Please type a space to go on, or B or Q to go ");
  201.          PUT(Prnt, "back to the question.");
  202.       else
  203.          Process_Char;
  204.       end if;
  205.    else
  206.       Process_Char;
  207.    end if;
  208.    Hpos := Hpos + 1;
  209.    if Hpos > Block'Length then
  210.       Vpos := Vpos + 1;
  211.       Hpos := 1;
  212.       Random_IO.Read(Data_File, Block, From => Random_IO.Count(Vpos));
  213.    end if;
  214. end Screen_Char;
  215.  
  216. separate (Book.Print_Screen.Screen_Char)
  217. procedure Process_Char is
  218. begin
  219.    case Block(Hpos) is
  220.       when '{'    => Put_Line(Prnt, Out1(1 .. Place - 1));
  221.                      Limit := Out2'Last;
  222.                      while Limit > 0 and then Out2(Limit) = ' ' loop
  223.                         Limit := Limit - 1;
  224.                      end loop;
  225.                      Put_Line(Prnt, Out2(1 .. Limit));
  226.                      Out2 := (others => ' ');
  227.                      Line_Num := Line_Num + 1;
  228.                      Out1 := (others => ' ');
  229.                      Place := 1;
  230.       when '@'    => Expanding := True;                  -- @ = several spaces.
  231.       when '^'    => Show(' ');                          -- ^ = bright + space.
  232.                      Bold := True;
  233.       when '~'    => Bold := False;                      -- ~ = normal + space.
  234.                      Show(' ');
  235.       when '%'    => Bold := True;                       -- % = bright.
  236.       when '`'    => Bold := False;                      -- ` = normal.
  237.       when '}'    => for I in Line_Num .. 23 loop        -- } = go to line 24.
  238.                         New_Line(Prnt, 2);
  239.                      end loop;
  240.                      Prompting := True;
  241.       when '\'    => Show(' ');                          -- \ = rev. vid. + sp.
  242.       when '$'    => if SN = 103 then                    -- $ = screen #.
  243.                         Show(' '); Show('_'); Show('_'); Show('_');
  244.                      else
  245.                         Show('$');
  246.                      end if;
  247.       when '#'    => if SN = 103 then                    -- # = % completed.
  248.                         Show(' '); Show('_'); Show('_');
  249.                      else
  250.                         Show('#');
  251.                      end if;
  252.       when others => Show(Block(Hpos));
  253.    end case;
  254. end Process_Char;
  255.  
  256. separate (Book.Print_Screen)
  257. procedure End_Of_Screen is
  258.    Ctrl_Info : Block_Subtype;          -- Control info. for the current screen.
  259.    I         : Integer;                     -- Used to index through Ctrl_Info.
  260. begin
  261.    Put_Line(Prnt, Out1(1 .. Place - 1));
  262.    Limit := Out2'Last;
  263.    while Limit > 0 and then Out2(Limit) = ' ' loop
  264.       Limit := Limit - 1;
  265.    end loop;
  266.    Put_Line(Prnt, Out2(1 .. Limit));
  267.    Place := 1;
  268.    while Block(Hpos) /= ']' loop    -- Read control information from Data File.
  269.       Hpos := Hpos + 1;
  270.       if Hpos > Block'Length then
  271.          Vpos := Vpos + 1;
  272.          Hpos := 1;
  273.          Random_IO.Read(Data_File, Block, From => Random_IO.Count(Vpos));
  274.       end if;
  275.       Ctrl_Info(Place) := Block(Hpos);
  276.       Place := Place + 1;
  277.    end loop;
  278.    if Ctrl_Info(1 .. Place - 1) = "]" then
  279.       Put_Line(Prnt, "(Program ends after this screen.)");
  280.    elsif Ctrl_Info(1 .. Place - 1) = "#]" then
  281.       Put_Line(Prnt, "(User types the next screen number.)");
  282.    else
  283.       I := 1;
  284.       while I + 4 < Place loop
  285.          Put(Prnt, "  '" & Ctrl_Info(I) & "' " & Ctrl_Info(I+1..I+3));
  286.          I := I + 4;
  287.          if I = 33 then
  288.             New_Line(Prnt);
  289.          end if;
  290.       end loop;
  291.       New_Line(Prnt);
  292.    end if;
  293.    New_Page(Prnt);
  294.    New_Line(Prnt);
  295. end End_Of_Screen;
  296.